<?php
/**
 * 抽奖模型
 *
 */
namespace Admin\Model;
use Think\Model;
class RushBuyModel extends BaseModel{

	/**
	 * 获取抽奖活动记录
	 * @param  array  $data 过滤参数
	 * @return  
	 */
	public function getRecord($data = array(), $down = false)
	{
		$model = M('RushBuyRecord');
		$model->alias('r');
		//组装查询条件
		$where['r.is_deleted'] = 0;
		
		//活动名称
		if (isset($data['name']) and $data['name'] !== '') {
			$or['r.nickname'] = array('LIKE', '%'.$data['name'].'%');
			$or['r.phone'] = array('LIKE', '%'.$data['name'].'%');
			$or['r.account'] = array('LIKE', '%'.$data['name'].'%');
			$or['_logic'] = 'or';
			$where['_complex'] = $or;
		}
			
		//活动创建时间开始区间
		if (isset($data['start']) and $data['start']) {
			$where['r.add_time'][] = array('egt', strtotime($data['start']));
		}
		//活动创建时间结束区间
		if (isset($data['end']) and $data['end']) {
			$where['r.add_time'][] = array('elt', strtotime($data['end'].' 23:59:59'));
		}
		
		$total = $model->where($where)->count();
		$pageIndex = I('post.page') ?: 1;
		$pageSize = I('post.page_size') ?: 20;
		$page = pagination($total, $pageSize ,$pageIndex);
		if ($total) {
			if($down){
				$list = M('RushBuyRecord')->alias('r')->field('r.*,b.title,b.prize_name')
						->join('__RUSH_BUY__ b ON b.id = r.activity_id', 'LEFT')
			      		->where($where)->order('r.id desc')->select();
			}else{
				$list = M('RushBuyRecord')->alias('r')->field('r.*,b.title,b.prize_name')
						->join('__RUSH_BUY__ b ON b.id = r.activity_id', 'LEFT')
				      	->where($where)->order('r.id desc')
				      	->limit($page['offset'], $page['limit'])->select();
			  }
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);

	}

	public function getCouponList($id)
	{
		$map['is_deleted'] = 0;
		$map['rush_id'] = $id;
		$model = M("RushBuyCoupon");
		$total = $model->where($map)->count();
		$pageIndex = I('post.page') ?: 1;
		$pageSize = I('post.page_size') ?: 20;
		$page = pagination($total, $pageSize ,$pageIndex);
		if ($total) {
			$list = $model->alias('r')
			      ->where($map)->order('r.id desc')
			      ->limit($page['offset'], $page['limit'])->select();
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
				$list[$k]['is_used'] = $v['is_used'] == 1 ? '已使用' : '未使用';
			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);

	}

		/**
	 * 获取抽奖活动列表
	 * @param  array  $data 过滤参数
	 * @return  
	 */
	public function getList($data = array())
	{
		//组装查询条件
		$where['r.is_deleted'] = 0;
		
		//活动名称
		if (isset($data['keyword']) and $data['keyword'] !== '') {
			$or['r.title'] = array('like', '%'.$data['keyword'].'%');
			$or['r.sub_title'] = array('LIKE', '%'.$data['keyword'].'%');
			$or['r.prize_name'] = array('LIKE', '%'.$data['keyword'].'%');
			$or['_logic'] = 'or';
			$where['_complex'] = $or;
			//$where['r.title'] = array('LIKE', '%'.$data['title'].'%');
		}
		if (isset($data['id']) and $data['id'] > 0) {
			$where['r.id'] = $data['id'];
		}	
		//活动创建时间开始区间
		if (isset($data['start']) and $data['start']) {
			$where['r.add_time'][] = array('egt', strtotime($data['start']));
		}
		//活动创建时间结束区间
		if (isset($data['end']) and $data['end']) {
			$where['r.add_time'][] = array('elt', strtotime($data['end'].' 23:59:59'));
		}
		
		$total = $this->alias('r')->where($where)->count();
		$pageIndex = I('post.page') ?: 1;
		$pageSize = I('post.page_size') ?: 20;
		$page = pagination($total, $pageSize ,$pageIndex);
		if ($total) {
			$list = $this->alias('r')
			      ->where($where)->order('r.id desc')
			      ->limit($page['offset'], $page['limit'])->select();
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
				$list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
				$list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);

			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);

	}

	public function addRush($data)
	{
		if($data['title'] == ''){
			$this->setErrMsg(10001, '请填写活动名称');
			return false;
		}
		if ($data['sub_title'] == '') {
			$this->setErrMsg(10002, '请填写活动子标题');
			return false;
		}
		if ($data['prize_name'] == ''){
			$this->setErrMsg(10003, '请填写奖品名称');
			return false;
		}
		if ($data['price'] <= 0) {
			$this->setErrMsg(10004, '请填写正确的奖品价格');
			return false;
		}
		if ($data['start_time'] == '') {
			$this->setErrMsg(10005, '请填写活动开始时间');
			return false;
		}
		if ($data['end_time'] == '') {
			$this->setErrMsg(10006, '请填写活动结束时间');
			return false;
		}
		if ($data['link'] == ''){
			$this->setErrMsg(10007, '请填写活动链接');
			return false;
		}
		if ($data['logo'] == ''){
			$this->setErrMsg(10007, '请上传奖品图片');
			return false;
		}
		$data['add_time'] = time();
		$data['start_time'] = strtotime($data['start_time']);
		$data['end_time'] = strtotime($data['end_time']);
		if ($data['start_time'] >= $data['end_time']) {
			$this->setErrMsg(10008, '时间选择出错');
		}
		$res = $this->add($data);
		return $res;
	}

	public function updateRush($data)
	{
		if ($data['id'] <= 0) {
			$this->setErrMsg(20001,'活动ID不能为空');
			return false;
		}
		if($data['title'] == ''){
			$this->setErrMsg(10001, '请填写活动名称');
			return false;
		}
		if ($data['sub_title'] == '') {
			$this->setErrMsg(10002, '请填写活动子标题');
			return false;
		}
		if ($data['prize_name'] == ''){
			$this->setErrMsg(10003, '请填写奖品名称');
			return false;
		}
		if ($data['price'] <= 0) {
			$this->setErrMsg(10004, '请填写正确的奖品价格');
			return false;
		}
		if ($data['start_time'] == '') {
			$this->setErrMsg(10005, '请填写活动开始时间');
			return false;
		}
		if ($data['end_time'] == '') {
			$this->setErrMsg(10006, '请填写活动结束时间');
			return false;
		}
		if ($data['link'] == ''){
			$this->setErrMsg(10007, '请填写活动链接');
			return false;
		}
		if ($data['logo'] == ''){
			$this->setErrMsg(10007, '请上传奖品图片');
			return false;
		}
		//$data['add_time'] = time();
		$data['start_time'] = strtotime($data['start_time']);
		$data['end_time'] = strtotime($data['end_time']);
		if ($data['start_time'] >= $data['end_time']) {
			$this->setErrMsg(10008, '时间选择出错');
		}
		$res = $this->save($data);
		return $res;
	}


	//管理员列表
	public function getAdminList()
	{
		$model = M("RushBuyAdmin");
		//组装查询条件
		$where['is_deleted'] = 0;
		$total = $model->where($where)->count();
		$pageIndex = I('post.page') ?: 1;
		$pageSize = I('post.page_size') ?: 20;
		$page = pagination($total, $pageSize ,$pageIndex);
		if ($total) {
			$list = $model->where($where)->order('id desc')
			      ->limit($page['offset'], $page['limit'])->select();
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date("Y-m-d H:i:s", $v['add_time']);
				if($v['last_login_time']){
					$list[$k]['last_login_time'] = date("Y-m-d H:i:s", $v['last_login_time']);
				}else{
					$list[$k]['last_login_time'] = '';
				} 
			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);

	}

	//添加管理员
	public function addAdmin($data){
		$admin = session('rush_admin');
		if ($admin!= 'admin') {
			$this->setErrMsg(20002,'权限不够');
			return false;
		}
		//数据验证
		if($data['username'] == ''){
			$this->setErrMsg(10001, '请输入用户名');
			return false;
		}
		if(mb_strlen($data['username']) > 20){
			$this->setErrMsg(10002, '用户名过长');
			return false;
		}

		if ($data['password'] == '') {
			$this->setErrMsg(10003, '请输入密码');
			return false;
		}
		if (mb_strlen($data['password']) > 16) {
			$this->setErrMsg(10004, '密码过长');
			return false;
		}
		if ($data['password'] != $data['confirm_password']) {
			$this->setErrMsg(10005, '密码与确认密码不一致');
			return false;
		}
		if (mb_strlen($data['remark']) > 30) {
			$this->setErrMsg(10006, '备注过长');
			return false;
		}

		$model  = M("RushBuyAdmin");
		//是否已存在
		$map['username'] = $data['username'];
		$map['is_deleted'] = 0;
		$count = $model->where($map)->count();
		if ($count) {
			$this->setErrMsg(20001, '用户名已存在');
			return false;
		}
		//加密保存
		unset($data['confirm_password']);
		$data['salt'] = rand(1, 1000000);
		$data['password'] = strtolower(md5($data['password'].$data['salt']));
		$data['add_time'] = time();
		$res = $model->add($data);
		return $res;
	}


	//修改管理员
	public function saveAdmin($data){
		$admin = session('rush_admin');
		if($data['id'] <= 0){
			$this->setErrMsg(10001, '参数错误');
			return false;
		}
		if (mb_strlen($data['new_password']) > 16) {
			$this->setErrMsg(10006, '密码过长');
			return false;
		}
		if ($data['new_password'] != $data['confirm_password']) {
				$this->setErrMsg(10005, '密码与确认密码不一致');
				return false;
		}

		$model = M("RushBuyAdmin");
		$map['id'] = $data['id'];
		$map['is_deleted'] = 0;
		$info = $model->where($map)->find();
		if(!$info){
			$this->setErrMsg(10002, '用户不存在');
			return false;
		}

		if($info['username'] != $admin){
			$this->setErrMsg(10003, '不能修改其他用户的信息');
			return false;
		}
		$pass = strtolower(md5($data['password'].$info['salt']));
		if($pass != $info['password']){
			$this->setErrMsg(20001, '密码错误');
			return false;
		}
			
		//加密保存
		unset($data['confirm_password']);
		$data['password'] = strtolower(md5($data['new_password'].$info['salt']));
		unset($data['new_password']);
		$data['add_time'] = time();
		$res = $model->save($data);
		if ($res !== false) {
			return true;
		}else{
			$this->setErrMsg(50000, '更新出错');
			return false;
		}
	}

	public function delAdmin($id)
	{
		$admin = session('rush_admin');
		if ($admin!= 'admin') {
			$this->setErrMsg(20002,'权限不够');
			return false;
		}

		$model = M("RushBuyAdmin");
		$map['id'] = $id;
		$map['is_deleted'] = 0;
		$info = $model->where($map)->find();
		if (!$info) {
			$this->setErrMsg(10001, '要删除的用户不存在');
			return false;
		}
		if ($info['username'] == 'admin'){
			$this->setErrMsg(10002, '该用户无法删除');
			return false;
		}
		$res = $model->where($map)->save(array('is_deleted'=>1));
		return $res;
	}

}
?>